package handler

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"moon-test/cache"
	"moon-test/model"
	"strconv"
	"time"
)

func GoodAdd(c *gin.Context) {
	goodCode := c.PostForm("goodCode")
	goodName := c.PostForm("goodName")
	goodType := c.PostForm("goodType")
	data := model.Good{
		GoodCode: goodCode,
		GoodName: goodName,
		GoodType: goodType,
	}
	err := model.DB.Create(&data).Debug().Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func OrderAdd(c *gin.Context) {
	orderCode := c.PostForm("orderCode")
	orderFrom := c.PostForm("orderFrom")
	orderTo := c.PostForm("orderTo")
	gid, _ := strconv.Atoi(c.PostForm("gid"))
	data := model.GoodOrder{
		OrderCode: orderCode,
		OrderFrom: orderFrom,
		OrderTo:   orderTo,
		Gid:       gid,
	}
	err := model.DB.Create(&data).Debug().Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func GoodOrderList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "3"))
	offset := (page - 1) * size
	var list []model.GoodOrderList
	/*
			SELECT
		  good_orders.id AS OrderId,
		  goods.good_code AS GoodCode,
		  goods.good_name AS GoodName,
		  goods.good_type AS GoodType,
		  good_orders.order_code AS OrderCode,
		  good_orders.order_from AS OrderFrom,
		  good_orders.order_to AS OrderTo
		FROM
		  good_orders
		  LEFT JOIN goods ON goods.id = good_orders.gid
	*/
	redis_key := "good_order_list" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		model.DB.Table("good_orders").
			Select(
				"good_orders.id AS OrderId",
				"goods.good_code AS GoodCode",
				"goods.good_name AS GoodName",
				"goods.good_type AS GoodType",
				"good_orders.order_code AS OrderCode",
				"good_orders.order_from AS OrderFrom",
				"good_orders.order_to AS OrderTo",
			).
			Joins("LEFT JOIN goods ON goods.id = good_orders.gid").
			Offset(offset).
			Limit(size).
			Debug().
			Find(&list)
		jsonData, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &list)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "获取成功",
		"data": list,
	})
	return
}

func GoodOrderDetail(c *gin.Context) {
	id := c.Query("id")
	var list model.GoodOrderList
	/*
			SELECT
		  good_orders.id AS OrderId,
		  goods.good_code AS GoodCode,
		  goods.good_name AS GoodName,
		  goods.good_type AS GoodType,
		  good_orders.order_code AS OrderCode,
		  good_orders.order_from AS OrderFrom,
		  good_orders.order_to AS OrderTo
		FROM
		  good_orders
		  LEFT JOIN goods ON goods.id = good_orders.gid
	*/
	redis_key := "good_order_detail" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		model.DB.Table("good_orders").
			Select(
				"good_orders.id AS OrderId",
				"goods.good_code AS GoodCode",
				"goods.good_name AS GoodName",
				"goods.good_type AS GoodType",
				"good_orders.order_code AS OrderCode",
				"good_orders.order_from AS OrderFrom",
				"good_orders.order_to AS OrderTo",
			).
			Joins("LEFT JOIN goods ON goods.id = good_orders.gid").
			Where("good_orders.id = ?", id).
			Debug().
			Find(&list)
		jsonData, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &list)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "获取成功",
		"data": list,
	})
	return
}
